package common

import (
	"hundsun.com/hsl/hschain/common/types"
)

// CreateFunc 共识初始化方法
type CreateFunc func() ConsensusModule

var (
	consensusDrivers = make(map[string]CreateFunc)
)

// RegConsensus 注册共识方法
func RegConsensus(name string, create CreateFunc) {
	if create == nil {
		panic("consensus create func is nil")
	}
	if _, dup := consensusDrivers[name]; dup {
		panic("consensus drivers register duplicately.")
	}
	consensusDrivers[name] = create
}

// LoadConsensus 加载共识方法
func LoadConsensus(name string) (CreateFunc, error) {
	if create, ok := consensusDrivers[name]; ok {
		return create, nil
	}
	return nil, types.ErrNotFound
}
